package com.xierong.remoteplayerlauncher.utils;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ExteralStorageUtils {
    private volatile static ExteralStorageUtils instance = null;
    private ExteralStorageUtils() {}

    public static ExteralStorageUtils getInstance() {
        if (instance == null) {
            synchronized (ExteralStorageUtils.class) {
                if (instance == null) {
                    instance = new ExteralStorageUtils();
                }
            }
        }
        return instance;
    }
    public String getExternalConfigFile(Context context) {
        String configPath = null;
        String sdcardPath = null;
        try{
            sdcardPath = getStorageDirectories(context)[0];
        } catch (Exception e) {
            Log.d(PlayerManager.TAG,"没有外部SD卡");
            return null;
        }
        if(sdcardPath == null) {
            Log.d(PlayerManager.TAG,"没有外部SD卡");
        } else {
            File sdcardDir = new File(sdcardPath);
            File[] files = sdcardDir.listFiles();
            if (files == null) {
                Log.d(PlayerManager.TAG,"没有找到任何文件");
            } else {
                for(int i = 0;i<files.length;i++) {
                    File f = files[i];
                    if (f.isDirectory()) {
                        File[] testFile = f.listFiles();
                        for(int j = 0;j<testFile.length;j++) {
                            if (testFile[j].isFile()) {
                                String fileName = testFile[j].getName();
                                if (fileName.equals("playlist.xml")) {
                                    configPath = testFile[j].getAbsolutePath();
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        return configPath;
    }
    private String[] getStorageDirectories(Context context) throws ArrayIndexOutOfBoundsException{
        String [] storageDirectories;
        String rawSecondaryStoragesStr = System.getenv("SECONDARY_STORAGE");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            List<String> results = new ArrayList<String>();
            File[] externalDirs = context.getExternalFilesDirs(null);
            for (File file : externalDirs) {
                String path = file.getPath().split("/Android")[0];
                if((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Environment.isExternalStorageRemovable(file))
                        || rawSecondaryStoragesStr != null && rawSecondaryStoragesStr.contains(path)){
                    results.add(path);
                }
            }
            storageDirectories = results.toArray(new String[0]);
        }else{
            final Set<String> rv = new HashSet<>();

            if (!TextUtils.isEmpty(rawSecondaryStoragesStr)) {
                final String[] rawSecondaryStorages = rawSecondaryStoragesStr.split(File.pathSeparator);
                Collections.addAll(rv, rawSecondaryStorages);
            }
            storageDirectories = rv.toArray(new String[rv.size()]);
        }
        return storageDirectories;
    }
}
